package minijava;

import minijava.parser.*;
import minijava.lexer.*;
import minijava.node.*;
import minijava.ASTAnalyser;
import java.io.*;

public class Compiler{
    public static void main(String[] arguments){

	if(arguments.length < 1){
	    System.out.println("No input files");
	    return;
	}

	try{

	    // Create a Parser instance
	    Lexer l = new Lexer(new PushbackReader(new FileReader(arguments[0])));
	    // Print the tokens
	    // printTokens(l);

	    Parser p = new Parser(l);

	    // Parse the input
	    Start tree = p.parse();

	    // PrettyPrint
	    tree.apply(new PrettyPrint());

	    // Look for errors in the AST
	    ASTAnalyser a = new ASTAnalyser();
	    tree.apply(a);
	    a.validateClasses();

	}catch(Exception e){
	    System.out.println(e.getMessage());
	}
    }

    public static void printTokens(Lexer lexer){
	try{
	    Token token = lexer.next();
	    boolean lastTokenBlank = true;
	    while(!(token instanceof EOF)){
		if(token instanceof TBlank || token instanceof TComment1 || token instanceof TComment2){
		    System.out.print(token.getText()+"[" + token.getLine()+']');
		    lastTokenBlank = true;
		}else{
		    if(!lastTokenBlank)
			System.out.print(" ");
		    String s = token.getClass().getName();
		    s = (s.substring(s.lastIndexOf('.')+2)).toUpperCase();
		    System.out.print(s);

		    // Arguments to be printed?
		    if(token instanceof TId || token instanceof TInteger)
			System.out.print('('+token.getText()+")[" + token.getLine()+']');

		    lastTokenBlank = false;
		}
		token = lexer.next();
	    }
	}catch(Exception e){
	    System.out.println(e.getMessage());
	}
    }
}
